package com.yanqu.road.server.manger.activity.ad;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ad.AdActivityModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.List;
import java.util.Map;

public class AdActivityMgr extends TempMgr {
    private static AdActivityConfig adActivityConfig;
    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadActivityData(){
        getLogger().info("reload ad activity start");
        List<ActivityInfo> activityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.AdActivity.getValue());
        if(activityInfoList.size() <= 0){
            adActivityConfig = null;
            getLogger().info("no ad activity in show time");
            return;
        }
        ActivityInfo activityInfo = activityInfoList.get(0);

        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
        AdActivityConfig tempConfig = new AdActivityConfig(activityInfo, activityConfigMap);
        adActivityConfig = tempConfig;

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            AdActivityModule adActivityModule = player.getModule(AdActivityModule.class);
            adActivityModule.initUserData();
            adActivityModule.syncUserData();
        }
        getLogger().info("reload ad activity end");
    }

    public static AdActivityConfig getAdActivityConfig(int activityId) {
        if(null != adActivityConfig && adActivityConfig.getActivityId() == activityId){
            ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
            if(ActivityHelper.activityInShowTime(activityInfo)) {
                return adActivityConfig;
            }
        }
        return null;
    }

    public static AdActivityConfig getAdActivityConfig() {
        if(null != adActivityConfig){
            return getAdActivityConfig(adActivityConfig.getActivityId());
        }
        return null;
    }
}
